angular.module('app.controllers')
    .controller('LoginCtrl', [
                '$scope', '$http', '$state', '$stateParams', 'alertService', 'oauthService',
        function($scope, $http, $state, $stateParams, alertService, oauthService) {
            $scope.form = {
                openId: $stateParams.openId,
                userName: '',
                password: ''
            };

            $scope.submit = function() {
                if ($scope.form.userName === '' || $scope.form.password === '') {
                    alertService.add('请输入用户名和密码');
                    return;
                }
                oauthService.getUserTokenAsync($scope.form.userName, $scope.form.password, $scope.form.openId)
                    .then(function(result) {
                        if (result.success) $state.go('app.home');
                        else {
                            alertService.add(result.error);
                        }
                    }, alertService.error);
            }
        }]);